import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router'

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json',
    Authorization: 'my-auth-token'
  })
};
@Component({
  selector: 'app-contact-list',
  templateUrl: './contact-list.component.html',
  styleUrls: ['./contact-list.component.css']
})
export class ContactListComponent implements OnInit {

  constructor(
    private http:HttpClient,
    private router:Router
  ) { }
public contacts:any 
  ngOnInit(): void {
   this.getContact()
  }
  getContact(){
    const token = window.localStorage.getItem("auth_token")
    this.http.get('http://localhost:3000/contacts'//,{headers: new HttpHeaders({
    //   "X-Access-Token":token})
    
    ).toPromise().then((data)=>{
console.log(data)
this.contacts = data

    })
  }
  deleteContact(id,e){
    e.preventDefault()
  if  (window.confirm("确定要删除此联系人吗？"))
    {this.http.delete('http://localhost:3000/contacts/'+id).toPromise().then((data)=>{
      console.log(data)
      window.alert("删除成功")
    }).catch((e)=>{
      console.log(e)

      window.alert("删除失败，请稍后重试")

    })
    // this.router.navigate(["/contact"])
this.getContact()}else{
return
}
  }

}
